import CreateRemindertip from "./ReminderTip";
import { newSessionid, getCatesian3FromPX ,createRemindertip,destroyTip} from "../../plotCommon";
import imageIcon from "./default.png";
import EditBillboard from './EditBillboard'
const Cesium = window.Cesium;
interface PointDraw {
  code?: String;
  position?: any;
  size?: number;
  decription?: any;
  style?: any;
  ground?: any;
  text?: any;
  crossOffset?: any;
  verticalOffset?: any;
  image?: any;
}
export default class Billboard {


  _param: PointDraw;
  _entities:Array<any>;
  pickerEntity:any;
  editBillborad:any;
  handler:any;
  _viewer:any
  _editEntity:any

  constructor(viewer:any) {
    this._viewer=viewer
    this._param = {
      code: "",
      position: "",
      size: 0.5,
      decription: "",
      style: "#ff4500",
      ground: "",
      text: "",
      crossOffset: 10,
      verticalOffset: 10,
      image:''
    };
    this._entities=[]
    this.handler= undefined
  }
  get options() {
    return this._param;
  }
  set options(val) {
    val=Object.assign({},val)
    this._param = val;
    //如果在编辑
    if(this._editEntity){
        console.log(val,'变话');
        let e=this._editEntity
        e.code=val.code
        e.label.text=val.text
        e.label.fillColor= this.hexToCesiumColor(val.style, 1),
        e.billboard.scale=val.size
        e.billboard.heightReference=val.ground
        ? Cesium.HeightReference.CLAMP_TO_GROUND
        : Cesium.HeightReference.NONE,
        e.label.pixelOffset=new Cesium.Cartesian2(val.crossOffset, val.verticalOffset)
  }
  }
  get entities(){
    return this._entities
  }
  set entities(val) {
    this._entities = val;

  }
  create(
    options?: PointDraw,
    callback?: Function
  ) {
    let {_viewer} =this
    
    // let viewer=this._viewer
    this.handler=new Cesium.ScreenSpaceEventHandler(_viewer.scene.canvas);
    // let handler=this.handler
    const toolTip = "左键点击选择点位,右键结束";
    this.handler.setInputAction((event: any) => {
      let { size, code, text, position,crossOffset,decription, ground, verticalOffset, style,image } =this._param;
      let pixPos = event.position;
      let cartesian = position||getCatesian3FromPX(_viewer, pixPos);
     let imageURl=Cesium.defaultValue(image,imageIcon);
      const point = _viewer.entities.add({
        code: code,
        id: newSessionid(),
        name: "Billboard",
        orientation: new Cesium.Quaternion(0, 0, 0, 0),
        position: cartesian,
        decription:decription,
        billboard: {
          image: imageURl,
          scale: size,
          horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
          verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
          scaleByDistance: new Cesium.NearFarScalar(1.5e1, 1.2, 1.5e5, 0.1),
          heightReference: ground
            ? Cesium.HeightReference.CLAMP_TO_GROUND
            : Cesium.HeightReference.NONE,
        },
        label: {
          text: text,
          font: "25px",
          fillColor: this.hexToCesiumColor(style, 1),
          outlineWidth: 1,
          pixelOffset: new Cesium.Cartesian2(crossOffset, verticalOffset),
          eyeOffset: new Cesium.Cartesian3(0, 0, -10),
        },
      });
      point.GeoType = "Billboard"; //记录对象的类型，用户后续编辑等操作
      point.PottingPoint = cartesian; //记录对象的节点数据，用户后续编辑等操作
      point.Editable = true; //代表当前对象可编辑,false状态下不可编辑

      this._entities.push(point)
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    this.handler.setInputAction((movement: any) => {
      console.log("鼠标移动事件监测：------创建点------");
      let endPos = movement.endPosition;
      if ( this.handler.isDestroyed())
        CreateRemindertip(toolTip, movement.position, false);
      CreateRemindertip(toolTip, endPos, true);
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
    this.handler.setInputAction((movementRight: any) => {
        this.handler.destroy();
      CreateRemindertip(toolTip, movementRight.position, false);
    }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
  }

  edit(editCalback:any) {
  let  viewer =this._viewer
   if(this.handler)this.handler.destroy();
    this.handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    const calback = (e:any) => {
        if (editCalback && typeof editCalback == "function")editCalback(e)
        this._editEntity=e 

      if (this.editBillborad) this.editBillborad.destroy();
      this.getPicking(this.handler, viewer, calback, false);
      this.editBillborad = new EditBillboard(viewer, e,1000,1);
      this.editBillborad.editTranslation();
    };
    const openTripParemeter = "code";
    this.getPicking(this.handler, viewer, calback, false);
  }
  stopCreate(){
    destroyTip()
    if (this.handler) this.handler.destroy();
  }
  stopEdit(){
    destroyTip()
    if (this.handler) this.handler.destroy();
    if (this.editBillborad) this.editBillborad.destroy();
  }
  removeById(id:any){
  this._viewer.entities.removeById(id)
  
  this._entities=this._entities.filter(item=>item.id!=id)
  }
  hexToCesiumColor(hex:any, alpha:any) {
    // 去除可能存在的 # 符号
    hex = hex.replace("#", "");

    // 提取红色、绿色和蓝色分量
    var red = parseInt(hex.substring(0, 2), 16) / 255;
    var green = parseInt(hex.substring(2, 4), 16) / 255;
    var blue = parseInt(hex.substring(4, 6), 16) / 255;

    // 创建并返回 Cesium.Color 对象
    return new Cesium.Color(red, green, blue, alpha);
  }
  getPicking(handler:any, viewer:any, callback:any, openTripParemeter:any) {
    // 创建一个ScreenSpaceEventHandler对象
    let _this = this;
    handler.setInputAction(function (click:any) {
      console.log("getPicking正在拾取坐标");

      var position = click.position;
      var pickedObject = viewer.scene.pick(position);
      // 检查pickedObject是否为实体
      if (
        Cesium.defined(pickedObject) &&
        Cesium.defined(pickedObject.id) &&
        pickedObject.id instanceof Cesium.Entity
      ) {
        // 获取点击的实体+
        var entity = pickedObject.id;
        // 输出实体信息
        const toolTip = pickedObject.id;
        _this.pickerEntity = entity;
        if (callback && typeof callback == "function") callback(entity);
        if (openTripParemeter && typeof openTripParemeter == "string")
          createRemindertip(
            entity[openTripParemeter],
            click.position,
            true
          );
      }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  }
}
